package components {
	import mx.binding.utils.BindingUtils;
	import mx.containers.HBox;
	import mx.controls.Label;
	import mx.controls.Text;
	import mx.controls.TextArea;
	import mx.controls.TextInput;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;

	public class FullViewItem extends HBox {
		
        //------------------------------------------------------------
        //
        // variables
        //
        //------------------------------------------------------------

        //------------------------------
        // labelText
        //------------------------------

        /**
         * label text
         */
        private var _labelText:String;

        public function get labelText():String {
        	return _labelText;
        }

        public function set labelText(labelText:String):void {
        	_labelText = labelText;
        }

        //------------------------------
        // contentText
        //------------------------------

        /**
         * content text
         */
        private var _contentText:String;

        public function get contentText():String {
        	return _contentText;
        }

        public function set contentText(contentText:String):void {
        	_contentText = contentText;
        }
        
        //------------------------------
        // editable
        //------------------------------

        /**
         * create editable controls or un-editable controls
         */
        private var _editable:Boolean;
        
        //------------------------------
        // multiLine
        //------------------------------

        /**
         * create editable controls or un-editable controls
         */
        private var _multiLine:Boolean;
        
		//------------------------------
        // host
        //------------------------------

        /**
         * the object that hosts the property or property chain to be watched
         */
		private var _host:Object;
		
		//------------------------------
        // chain
        //------------------------------

        /**
         * a value specifying the property or chain to be watched
         */
		private var _chain:String;

        //------------------------------------------------------------
        //
        // constructor
        //
        //------------------------------------------------------------

		/**
         * the content will be bound with assigned control property
         * @param label label of the item
         * @param content content of the item
         * @param host the object that hosts the property or property chain to be watched
         * @param chain a value specifying the property or chain to be watched
         * @param editable determite the content is editable or not
         */
		public function FullViewItem(labelText:String, contentText:String,
									 host:Object = null, chain:String = null,
									 editable:Boolean = false,
									 multiLine:Boolean = false) {
			super();
			
			percentWidth = 100;
			
			_labelText = labelText;
			_contentText = contentText;
			_host = host;
			_chain = chain;
			_editable = editable;
			_multiLine = multiLine;
			
			addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
		}
		
		//------------------------------------------------------------
        //
        // event handlers
        //
        //------------------------------------------------------------
		
		public function onCreationComplete(e:FlexEvent):void {
			var contentCtrl:UIComponent;
			
			// label control
			var labelCtrl:Label = new Label();
			labelCtrl.width = 58;
			labelCtrl.text = labelText;
			labelCtrl.setStyle("fontWeight", "bold");
			addChild(labelCtrl);
			
			// content control
			// if editable is true, create mx:Text for the content
			// if editable is false, create mx:TextInput of mx:TextArea for the content
			if (!_editable) {
				var textCtrl:Text = new Text();
				textCtrl.percentWidth = 100;
				textCtrl.text = contentText;
				
				if (_host) {
					BindingUtils.bindProperty(textCtrl, "text", _host, _chain);
				}
				
				addChild(textCtrl);
			} else {
				if (!_multiLine) {
					var eidtCtrl:TextInput = new TextInput();
					eidtCtrl.percentWidth = 100;
					eidtCtrl.text = contentText;
					
					if (_host) {
						BindingUtils.bindProperty(eidtCtrl, "text", _host, _chain);
						BindingUtils.bindProperty(_host, _chain, eidtCtrl, "text");
					}
					
					addChild(eidtCtrl);
				} else {
					var eidtAreaCtrl:TextArea = new TextArea();
					eidtAreaCtrl.percentWidth = 100;
					percentHeight = 100;
					eidtAreaCtrl.percentHeight = 100;
					eidtAreaCtrl.text = contentText;
					
					if (_host) {
						BindingUtils.bindProperty(eidtAreaCtrl, "text", _host, _chain);
						BindingUtils.bindProperty( _host, _chain, eidtAreaCtrl, "text");
					}
					
					addChild(eidtAreaCtrl);
				}
			}
		}
	}
}